SUBROUTINES Here's a simple program: 10 PRINT "THE" 20 GO TO 1000 30 PRINT "DEAD" 40 END 1000 PRINT "BIRD" 1010 PRINT "IS" 1020 GO TO 30 It makes the computer print: THE BIRD IS DEAD The program consists of two parts. The main part (lines 10-40) is called the main routine; the bottom part (lines 1000-1020) is called the subroutine. The bottom line of the main routine says END. Line 20, which is in the main routine, makes the computer skip to the subroutine. Line 1020, in the subroutine, makes the computer return to the main routine. To make the program more elegant, change lines 20 and 1020: 10 PRINT "THE" 20 GOSUB 1000 30 PRINT "DEAD" 40 END 1000 PRINT "BIRD" 1010 PRINT "IS" 1020 RETURN The new line 20 says: GO to the SUBroutine that begins at line 1000. The new line 1020 says: RETURN to the main routine, where you left off. Like the old program, the new program prints: THE BIRD IS DEAD GOSUB is like GO TO. The GOSUB 1000 means ``GO TO line 1000, and remember where you came from''; so the computer goes to line 1000, while remembering that it came from line 20. In line 1020, the RETURN means ``RETURN to where you came from''; so the computer returns to line 20, and then proceeds from line 20 to line 30, which prints the word ``DEAD''. Bottom lines The bottom line of a main routine should say END. The bottom line of a subroutine should say RETURN. Yankee Doodle This program prints the original version of Yankee Doodle: Print the first verse: 10 PRINT "FATHER AND I WENT DOWN TO CAMP" 20 PRINT "ALONG WITH CAPTAIN GOODING," 30 PRINT "AND THERE WE SAW THE MEN AND BOYS" 40 PRINT "AS THICK AS HASTY PUDDING." Do the chorus, and then come back: 50 GOSUB 1000 Print the second verse: 60 PRINT "AND THERE WAS CAPTAIN WASHINGTON" 70 PRINT "UPON A SLAPPING STALLION," 80 PRINT "A-GIVING ORDERS TO HIS MEN;" 90 PRINT "I GUESS THERE WERE A MILLION!" Do the chorus, and then come back: 100 GOSUB 1000 Print the third verse: 110 PRINT "THE FLAMING RIBBONS IN HIS HAT," 120 PRINT "THEY LOOKED SO TARNAL FINE, AH," 130 PRINT "I WANTED POCKILY TO GET" 140 PRINT "TO GIVE TO MY JEMIMAH." Do the chorus, and then come back: 150 GOSUB 1000 That's the end of the song: 160 END Here's the chorus: 1000 PRINT 1010 PRINT "YANKEE DOODLE, KEEP IT UP." 1020 PRINT "YANKEE DOODLE DANDY," 1030 PRINT "MIND THE MUSIC AND THE STEP," 1040 PRINT "AND WITH THE GIRLS BE HANDY." 1050 PRINT 1060 RETURN Lines 10-40 print the first verse. Lines 60-90 print the second. Lines 110-140 print the third. At the end of each verse, the computer is told to ``GOSUB 1000'', which means go to line 1000, print the chorus (lines 1000-1050), and then RETURN to where it left off. Lines 1000-1060, which print the chorus, are the subroutine. The verses (lines 10-160) are the main routine. Lines 1000 and 1050 make the computer print a blank line at the beginning and end of the chorus. The bottom line of the main routine is END. The bottom line of the subroutine is RETURN. If you accidentally omit line 160 (which says END), the computer will proceed to do the subroutine an extra time, and then won't know where to RETURN. It will gripe, by saying: RETURN BEFORE GOSUB To avoid such gripes, the last line of a main routine should be END. Love poem This program prints a love poem: 10 PRINT "THE MOST BEAUTIFUL THING" 20 PRINT "IN THE WHOLE WIDE WORLD" 30 PRINT "IS..." 40 PRINT "LOVE!" 50 PRINT "THE OPPOSITE OF HATE IS" 60 PRINT "LOVE!" 70 PRINT "THE OPPOSITE OF WAR IS" 80 PRINT "LOVE!" 90 PRINT "THE OPPOSITE OF DESPAIR IS" 100 PRINT "LOVE!" 110 PRINT "AND WHEN I LOOK AT YOU," 120 PRINT "I FEEL LOTS OF" 130 PRINT "LOVE!" In that program, lines 40, 60, 80, 100, and 130 print the word LOVE. Let's make those lines print the word LOVE larger, like this: * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * Here's how: 10 PRINT "THE MOST BEAUTIFUL THING" 20 PRINT "IN THE WHOLE WIDE WORLD" 30 PRINT "IS..." 40 GOSUB 1000 50 PRINT "THE OPPOSITE OF HATE IS" 60 GOSUB 1000 70 PRINT "THE OPPOSITE OF WAR IS" 80 GOSUB 1000 90 PRINT "THE OPPOSITE OF DESPAIR IS" 100 GOSUB 1000 110 PRINT "AND WHEN I LOOK AT YOU," 120 PRINT "I FEEL LOTS OF" 130 GOSUB 1000 140 END Lines 1000-10701000 PRINT "* * * * * * * * *" are the subroutine1010 PRINT "* * * * * *" 1020 PRINT "* * * * * * * *" 1030 PRINT "* * * * * *" 1040 PRINT "* * * * * * * * * * * *" 1050 PRINT 1060 PRINT 1070 RETURN In that new version, lines 40, 60, 80, 100, and 130 say GOSUB 1000 instead of PRINT ``LOVE!''. The GOSUB 1000 means: do the subroutine that begins at line 1000. The subroutine prints the word LOVE in large letters. Old lady Here are the lyrics to a famous song: There was an old lady who swallowed a fly, But I don't know why she swallowed the fly. Perhaps she'll die. There was an old lady who swallowed a spider That wiggled and jiggled and tickled inside her. She swallowed the spider to catch the fly, But I don't know why she swallowed the fly. Perhaps she'll die. There was an old lady who swallowed a bird. Oh, how absurd! To swallow a bird! She swallowed the bird to catch the spider That wiggled and jiggled and tickled inside her. She swallowed the spider to catch the fly, But I don't know why she swallowed the fly. Perhaps she'll die. There was an old lady who swallowed a cat. Imagine that! To swallow a cat! She swallowed the cat to catch the bird. She swallowed the bird to catch the spider That wiggled and jiggled and tickled inside her. She swallowed the spider to catch the fly, But I don't know why she swallowed the fly. Perhaps she'll die. There was an old lady who swallowed a dog. I swear on this log! She swallowed that dog! She swallowed the dog to catch the cat. She swallowed the cat to catch the bird. She swallowed the bird to catch the spider That wiggled and jiggled and tickled inside her. She swallowed the spider to catch the fly, But I don't know why she swallowed the fly. Perhaps she'll die. There was an old lady who swallowed a goat. Including its coat, she swallowed that goat! She swallowed the goat to catch the dog. She swallowed the dog to catch the cat. She swallowed the cat to catch the bird. She swallowed the bird to catch the spider That wiggled and jiggled and tickled inside her. She swallowed the spider to catch the fly, But I don't know why she swallowed the fly. Perhaps she'll die. There was an old lady who swallowed a cow. I don't know how she swallowed that cow! She swallowed the cow to catch the goat. She swallowed the goat to catch the dog. She swallowed the dog to catch the cat. She swallowed the cat to catch the bird. She swallowed the bird to catch the spider That wiggled and jiggled and tickled inside her. She swallowed the spider to catch the fly, But I don't know why she swallowed the fly. Perhaps she'll die. There was an old lady who swallowed a horse, And she died, of course! This program prints the song: 10 A$="THERE WAS AN OLD LADY WHO SWALLOWED A " 20 PRINT A$;"FLY.": GOSUB 1070 30 PRINT A$;"SPIDER": GOSUB 1050 40 PRINT A$;"BIRD.": PRINT "OH, HOW ABSURD! TO SWALLOW A BIRD!": GOSUB 1040 50 PRINT A$;"CAT.": PRINT "IMAGINE THAT! TO SWALLOW A CAT!": GOSUB 1030 60 PRINT A$;"DOG.": PRINT "I SWEAR ON THIS LOG! SHE SWALLOWED THAT DOG!": GOSUB 1020 70 PRINT A$;"GOAT.": PRINT "INCLUDING ITS COAT, SHE SWALLOWED THAT GOAT!": GOSUB 1010 80 PRINT A$;"COW.": PRINT "I DON'T KNOW HOW SHE SWALLOWED THAT COW!": GOSUB 1000 90 PRINT A$;"HORSE,": PRINT "AND SHE DIED, OF COURSE!" 100 END 1000 PRINT "SHE SWALLOWED THE COW TO CATCH THE GOAT." 1010 PRINT "SHE SWALLOWED THE GOAT TO CATCH THE DOG." 1020 PRINT "SHE SWALLOWED THE DOG TO CATCH THE CAT." 1030 PRINT "SHE SWALLOWED THE CAT TO CATCH THE BIRD." 1040 PRINT "SHE SWALLOWED THE BIRD TO CATCH THE SPIDER" 1050 PRINT "THAT WIGGLED AND JIGGLED AND TICKLED INSIDE HER." 1060 PRINT "SHE SWALLOWED THE SPIDER TO CATCH THE FLY," 1070 PRINT "BUT I DON'T KNOW WHY SHE SWALLOWED THE FLY." 1080 PRINT "PERHAPS SHE'LL DIE." 1090 PRINT 1100 RETURN Lines 1000-1100 are a subroutine. That subroutine tells the whole story. In line 20, the GOSUB says to do just part of the subroutine ___ the part that begins at line 1070. Line 30 says to do more of the subroutine ___ by beginning at line 1050. Lines 40, 50, 60, and 70 do progressively larger chunks of the subroutine. Line 80 is the climax: it says to do all of the subroutine. Line 90 provides a humorous ending. Nesting One subroutine can lead to another: Main routine: 10 PRINT "MY" 20 GOSUB 1000 30 PRINT "NOSE" 40 END Subroutine 1000: 1000 PRINT "TOE" 1010 PRINT "IS" 1020 GOSUB 2000 1030 PRINT "YOUR" 1040 RETURN Subroutine 2000: 2000 PRINT "STUCK" 2010 PRINT "IN" 2020 RETURN The program consists of three routines. The main routine ends with END; each subroutine ends with RETURN. The main routine consists of lines 10, 20, 30, and 40. Let's see what those lines do. Line 10 prints MY. Line 20 makes the computer do subroutine 1000, which prints TOE, prints IS, does subroutine 2000 (STUCK and IN), and prints YOUR. Line 30 prints NOSE. Line 40 ends. So altogether, the computer prints: MY TOE IS STUCK IN YOUR NOSE In that example, subroutine 2000 is nested in subroutine 1000. SUBSCRIPTS Instead of being a single string, X$ can be a whole list of strings, like this: "HATE" "LOVE" "KILL" X$= "KISS" "WAR" "PEACE" "WHY" Here's how to make X$ be that list of strings. . . . Begin your program by saying: 10 DIM X$(7) That says X$ will be a list of 7 strings. DIM means dimension; the line says the dimension of X$ is 7. Next, tell the computer what strings are in X$. Type these lines: 20 X$(1)="HATE" 30 X$(2)="LOVE" 40 X$(3)="KILL" 50 X$(4)="KISS" 60 X$(5)="WAR" 70 X$(6)="PEACE" 80 X$(7)="WHY" Line 20 says X$'s first string is HATE. Line 30 says X$'s second string is LOVE. The remaining lines define the other strings in X$. If you'd like the computer to print all those strings, type this: 90 FOR I = 1 TO 7: PRINT X$(I): NEXT That means: print all the strings in X$. The computer will print: HATE LOVE KILL KISS WAR PEACE WHY In that program, line 20 talks about X$(1). Instead of saying X$(1), math books say: X1 The ``1'' is called a subscript. Similarly, in line 30, which says X$(2)=``LOVE'', the number 2 is a subscript. Some programmers pronounce line 30 like this: ``X string, subscripted by 2, is LOVE''. Some programmers simply say: ``X string 2 is LOVE''. In that program, X$ is called an array (or matrix). Definition: an array (or matrix) is a variable that has subscripts. Subscripted DATA That program said X$(1) is HATE, and X$(2) is LOVE, and so on. This program does the same thing, more briefly: 10 DIM X$(7) 20 DATA HATE,LOVE,KILL,KISS,WAR,PEACE,WHY 30 FOR I=1 TO 7: READ X$(I): NEXT 40 FOR I=1 TO 7: PRINT X$(I): NEXT Line 10 says X$ will be a list of 7 strings. Line 20 contains a list of 7 strings. Line 30 makes the computer READ those strings and call them X$. Line 40 makes the computer print them. In that program, the first three lines say: DIM DATA FOR I Most practical programs begin with those three lines. Let's lengthen the program, so that the computer prints all this: HATE LOVE KILL KISS WAR PEACE WHY WHY HATE WHY LOVE WHY KILL WHY KISS WHY WAR WHY PEACE WHY WHY That consists of two verses. The second verse resembles the first verse, except that each line of the second verse begins with WHY. To make the computer print all that, just add these lines to the program: 50 PRINT 60 FOR I = 1 TO 7: PRINT "WHY ";X$(I): NEXT Line 50 leaves a blank line between the first verse and the second verse. Line 60 prints the second verse. Line 60 resembles line 40 (which printed the first verse), except that line 60 prints ``WHY '' before each X$(I). Let's add a third verse, which prints the words in reverse order: WHY PEACE WAR KISS KILL LOVE HATE Before printing that third verse, print a blank line: 70 PRINT Then print the verse itself. To print the verse, you must print X$(7), then print X$(6), then print X$(5), etc. To do that, you could say: 80 PRINT X$(7) 90 PRINT X$(6) 100 PRINT X$(5) etc. But this way is shorter: 80 FOR I = 7 TO 1 STEP -1: PRINT X$(I): NEXT Numeric arrays Let's make Y be this list of six numbers: 100, 26, 94, 201, 8.3, and -7. To begin, tell the computer that Y will consist of six numbers: 10 DIM Y(6) Next, tell the computer what the six numbers are: 20 DATA 100,26,94,201,8.3,-7 Make the computer READ all that data: 30 FOR I = 1 TO 6: READ Y(I): NEXT To make the computer PRINT all that data, type this: 40 FOR I = 1 TO 6: PRINT Y(I): NEXT If you want the computer to add those 6 numbers together and print their sum, say: 50 PRINT Y(1)+Y(2)+Y(3)+Y(4)+Y(5)+Y(6) Strange example Getting tired of X and Y? Then pick another letter! For example, you can play with Z: Silly, useless programWhat the program means 10 DIM Z(5) Z will be a list of 5 numbers. 20 FOR I = 2 TO 5 30 Z(I)=I*100 Z(2)=200. Z(3)=300. Z(4)=400. Z(5)=500. 40 NEXT 50 Z(1)=Z(2)-3 Z(1) is 200-3, so Z(1) is 197. 60 Z(3)=Z(1)-2 Z(3) changes to 197-2, which is 195. 70 FOR I = 1 TO 5: PRINT Z(I): NEXTPrint Z(1), Z(2), Z(3), Z(4), and Z(5). Line 70 prints: 197 200 195 400 500 Problems and solutions Suppose you want to analyze 50 numbers. Begin your program by saying: 10 DIM X(50) Then type the 50 numbers, as data, like this: 20 DATA etc. 30 DATA etc. 40 DATA etc. Tell the computer to READ the data: 100 FOR I=1 TO 50: READ X(I): NEXT After line 100, do one of the following, depending on which problem you want to solve. . . . Print all the values of X Solution: 110 FOR I = 1 TO 50: PRINT X(I): NEXT Print all the values of X, in reverse order Solution: 110 FOR I = 50 TO 1 STEP -1: PRINT X(I): NEXT Print the sum of all the values of X In other words, print X(1)+X(2)+X(3)+ . . . +X(50). Solution: start the sum at 0 ___ 110 S=0 and then increase the sum, by adding each X(I) to it: 120 FOR I = 1 TO 50: S=S+X(I): NEXT Finally, print the sum: 130 PRINT "THE SUM OF ALL THE NUMBERS IS";S Find the average of X In other words, find the average of the 50 numbers. Solution: begin by finding the sum ___ 110 S=0 120 FOR I = 1 TO 50: S=S+X(I): NEXT and then divide the sum by 50: 130 PRINT "THE AVERAGE IS";S/50 Find out whether any of the values of X is 79.4 In other words, find out whether 79.4 is a number in the list. Solution: if X(I) is 79.4, print ``YES'' ___ 110 FOR I = 1 TO 50 120 IF X(I)=79.4 THEN PRINT "YES, 79.4 IS IN THE LIST": END 130 NEXT otherwise, print ``NO'': 140 PRINT "NO, 79.4 IS NOT IN THE LIST" In X's list, count how often 79.4 appears Solution: start the counter at zero ___ 110 C=0 and increase the counter each time you see the number 79.4: 120 FOR I = 1 TO 50 130 IF X(I)=79.4 THEN C=C+1 140 NEXT Finally, print the counter: 150 PRINT "THE NUMBER 79.4 APPEARS";C;"TIMES" Print all the values of X that are negative In other words, print all the numbers that have minus signs. Solution: begin by announcing your purpose ___ 110 PRINT "HERE ARE THE VALUES THAT ARE NEGATIVE:" and then print the values that are negative; in other words, print each X(I) that's less than 0: 120 FOR I = 1 TO 50 130 IF X(I)<0 THEN PRINT X(I) 140 NEXT Print all the values of X that are ``above average'' Solution: find the average, and call it A, like this ___ 110 S=0 120 FOR I = 1 TO 50: S=S+X(I): NEXT 130 A=S/50 then announce your purpose: 140 PRINT "THE FOLLOWING VALUES ARE ABOVE AVERAGE:" Finally, print the values that are above average; in other words, print each X(I) that's greater than A: 150 FOR I = 1 TO 50 160 IF X(I)>A THEN PRINT X(I) 170 NEXT Find the biggest value of X In other words, find which of the 50 numbers is the biggest. Solution: let B stand for the biggest number. Begin by tentatively setting B equal to the first number ___ 110 B=X(1) but if another number is bigger than that B, change B: 120 FOR I = 2 TO 50 130 IF X(I)>B THEN B=X(I) 140 NEXT Afterwards, print B: 150 PRINT "THE BIGGEST NUMBER IN THE LIST IS";B Find the smallest value of X In other words, find which of the 50 numbers is the smallest. Solution: let S stand for the smallest number. Begin by tentatively setting S equal to the first number ___ 110 S=X(1) but if another number is smaller than S, change S: 120 FOR I = 2 TO 50 130 IF X(I)=X(I+1) THEN PRINT "NO, THE LIST IS NOT IN STRICTLY INCREASING OR DER": END 130 NEXT otherwise, print ``YES'': 140 PRINT "YES, THE LIST IS IN STRICTLY INCREASING ORDER" Test yourself: look at those problems again, and see whether you can figure out the solutions without peeking at the answers. Multiple arrays Suppose your program involves three lists. Suppose the first list is called A$ and consists of 18 strings; the second list is called B and consists of 57 numbers; and the third list is called C$ and consists of just 3 strings. To say all that, begin your program with this statement: 10 DIM A$(18),B(57),C$(3) Double subscripts You can make X be a table of strings, like this: "DOG" "CAT" "MOUSE" X$= "HOTDOG" "CATSUP" "MOUSETARD" Here's how to make X$ be that table. . . . Begin by saying: 10 DIM X$(2,3) That says X$ will be a table having 2 rows and 3 columns. Then tell the computer what strings are in X$. Type these lines: 20 X$(1,1)="DOG" 30 X$(1,2)="CAT" 40 X$(1,3)="MOUSE" 50 X$(2,1)="HOTDOG" 60 X$(2,2)="CATSUP" 70 X$(2,3)="MOUSETARD" Line 20 says: the string in X$'s first row and first column is DOG. Line 30 says the string in X$'s first row and second column is CAT. The remaining lines define the other strings in X$. If you'd like the computer to print all those strings, type this: 80 FOR I = 1 TO 2: FOR J = 1 TO 3: PRINT X$(I,J),: NEXT: PRINT: NEXT That means: print all the strings in X$. The computer will print: DOG CAT MOUSE HOTDOG CATSUP MOUSETARD In that program, X$ is called a table or two-dimensional array or doubly subscripted array. Multiplication table This program prints a multiplication table: 10 DIM X(10,4) 20 FOR I = 1 TO 10: FOR J = 1 TO 4: X(I,J)=I*J: NEXT: NEXT 30 FOR I = 1 TO 10: FOR J = 1 TO 4: PRINT X(I,J),: NEXT: PRINT: NEXT Line 10 says X will be a table having 10 rows and 4 columns. The middle of line 20 says X(I,J)=I*J. That means the number in row I and column J is I*J. For example, the number in row 3 and column 4 is 12. The beginning of line 20 says ``FOR I = 1 TO 10: FOR J = 1 TO 4'', so that X(I,J)=I*J for every I and J, so every entry in the table is defined by multiplication. Line 30 prints the whole table: 1 2 3 4 2 4 6 8 3 6 9 12 4 8 12 16 5 10 15 20 6 12 18 24 7 14 21 28 8 16 24 32 9 18 27 36 10 20 30 40 Instead of multiplication, you can have addition, subtraction, or division: just change line 20. Most programmers follow this tradition: the row's number is called I, and the column's number is called J. Line 20 obeys that tradition. Notice I comes before J in the alphabet; I comes before J in X(I,J); and ``FOR I'' comes before ``FOR J''. If you follow the I-before-J tradition, you'll make fewer errors. Summing a table Suppose you want to analyze this table: 32.7 19.4 31.6 85.1 -8 402 -61 0 5106 -.2 0 -1.1 36.9 .04 1 11 777 666 55.44 2 1.99 2.99 3.99 4.99 50 40 30 20 12 21 12 21 0 1000 2 500 Since the table has 9 rows and 4 columns, begin your program by saying: 10 DIM X(9,4) Each row of the table becomes a row of the DATA: 11 DATA 32.7, 19.4, 31.6, 85.1 12 DATA -8, 402, -61, 0 13 DATA 5106, -.2, 0, -1.1 14 DATA 36.9, .04, 1, 11 15 DATA 777, 666, 55.44, 2 16 DATA 1.99, 2.99, 3.99, 4.99 17 DATA 50, 40, 30, 20 18 DATA 12, 21, 12, 21 19 DATA 0, 1000, 2, 500 Make the computer READ the data: 20 FOR I = 1 TO 9: FOR J = 1 TO 4: READ X(I,J): NEXT: NEXT To make the computer print the table, say this: 30 FOR I = 1 TO 9: FOR J = 1 TO 4: PRINT X(I,J),: NEXT: PRINT: NEXT Here are some problems, with solutions. . . . Find the sum of all the numbers in the table Solution: start the sum at 0 ___ 100 S=0 and then increase the sum, by adding each X(I,J) to it: 110 FOR I = 1 TO 9: FOR J = 1 TO 4: S=S+X(I,J): NEXT: NEXT Finally, print the sum: 120 PRINT "THE SUM OF ALL THE NUMBERS IS";S The computer will print: THE SUM OF ALL THE NUMBERS IS 8877.84 Find the sum of each row In other words, make the computer print the sum of the numbers in the first row, then the sum of the numbers in the second row, then the sum of the numbers in the third row, etc. Solution: the general idea is ___ 100 FOR I = 1 TO 9 110 print the sum of row I 120 NEXT Here are the details: 110 FOR I = 1 TO 9 110 S=0 111 FOR J = 1 TO 4: S=S+X(I,J): NEXT 112 PRINT "THE SUM OF ROW";I;"IS";S 120 NEXT The computer will print: THE SUM OF ROW 1 IS 168.8 THE SUM OF ROW 2 IS 333 THE SUM OF ROW 3 IS 5104.7 etc. Find the sum of each column In other words, make the computer print the sum of the numbers in the first column, then the sum of the numbers in the second column, then the sum of the numbers in the third column, etc. Solution: the general idea is ___ 100 FOR J = 1 TO 4 110 print the sum of column J 120 NEXT Here are the details: 100 FOR J = 1 TO 4 110 S=0 111 FOR I = 1 TO 9: S=S+X(I,J): NEXT 112 PRINT "THE SUM OF COLUMN";J;"IS";S 120 NEXT The computer will print: THE SUM OF COLUMN 1 IS 6008.59 THE SUM OF COLUMN 2 IS 2151.23 THE SUM OF COLUMN 3 IS 75.03 THE SUM OF COLUMN 4 IS 642.99 In all the other examples, ``FOR I'' came before ``FOR J''; but in this unusual example, ``FOR I'' comes after ``FOR J''.